package cn.com.cifi.mars.mapper.wrapper;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.com.cifi.mars.entity.ProjectReturnCoefficient;
import cn.com.cifi.mars.mapper.mars.ProjectReturnCoefficientMapper;

/**
 * @author: JingYuanXiang
 * @date: 2020/11/9 16:05
 */
@Component
public class ProjectReturnCoefficientWrapper {
    @Autowired
    private ProjectReturnCoefficientMapper returnCoefficientMapper;
    /**
     * 查询回款系数版本数量
     * @param projectId 项目id
     * @param versionType 版本类型
     * @param versionState 版本状态
     * @param isDelete 是否删除
     * @return
     */
    public Integer selectCountByProjectIdAndVersionTypeGroupByAffiliationMonth(String projectId, String versionType,String versionState, int isDelete) {
        QueryWrapper<ProjectReturnCoefficient> queryWrapper = new QueryWrapper();
        queryWrapper.eq("project_id",projectId)
                .eq("version_type",versionType)
                .eq("version_state", versionState)
                .eq("is_delete", isDelete)
                .groupBy("affiliation_month");
        return returnCoefficientMapper.selectCount(queryWrapper);
    }

    /**
     * 查询最新回款系数版本
     * @param projectId 项目id
     * @param versionType 版本类型
     * @param versionState 版本状态
     * @param isDelete 是否删除
     * @return
     */
    public ProjectReturnCoefficient selectLatestByProjectIdAndVersionTypeAndVersionState(String projectId, String versionType, String versionState, int isDelete){
        QueryWrapper<ProjectReturnCoefficient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",projectId)
                .eq("version_type", versionType)
                .eq("version_state", versionState)
                .eq("is_delete", isDelete)
                .orderByDesc("create_time")
                .last("limit 1");
        return returnCoefficientMapper.selectOne(queryWrapper);
    }


    public List<ProjectReturnCoefficient> selectCountByProjectIdAndVersionType(String projectId, String type, int isDelete) {
        QueryWrapper<ProjectReturnCoefficient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",projectId)
                .eq("version_type", type)
                .eq("is_delete", isDelete)
                .orderByDesc("create_time");
        return returnCoefficientMapper.selectList(queryWrapper);
    }

    public ProjectReturnCoefficient selectById(String versionId, int isDelete) {
        QueryWrapper<ProjectReturnCoefficient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",versionId)
                .eq("is_delete", isDelete);
        return returnCoefficientMapper.selectOne(queryWrapper);
    }


    public ProjectReturnCoefficient selectNewestByProjectId(String projectId,String versionType, int isDelete) {
        QueryWrapper<ProjectReturnCoefficient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",projectId)
                .eq("version_type", versionType)
                .eq("is_delete", isDelete)
                .orderByDesc("create_time")
                .last("limit 1");
        return returnCoefficientMapper.selectOne(queryWrapper);
    }

    public ProjectReturnCoefficient selectNewestPracticalByProjectId(String projectId, String versionType, int isDelete, String versionState) {
        QueryWrapper<ProjectReturnCoefficient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",projectId)
                .eq("version_type", versionType)
                .eq("is_delete", isDelete)
                .eq("version_state",versionState)
                .orderByDesc("create_time")
                .last("limit 1");
        return returnCoefficientMapper.selectOne(queryWrapper);
    }


}
